#!/usr/python3.6.3/bin/python3
# -*- coding: utf-8 -*-

from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText
import time
import sys

i = 1


def SendEamil():
        mail_info = {
                "from": "*****@139.com",
                "to": "******@139.com",
                "hostname": "smtp.139.com",
                "username": "*******@139.com",
                "password": "*******",
                "mail_subject": "发布代码故障",
                "mail_text": "告警项目:{} \n问题详情:{}".format(
                        sys.argv[1], sys.argv[2]
                    ),
                "mail_encoding": "utf-8"
        }
        #这里使用SMTP_SSL就是默认使用465端口
        smtp = SMTP_SSL(mail_info["hostname"], 465)
# debug模式
#       smtp.set_debuglevel(1)

        smtp.ehlo(mail_info["hostname"])
        smtp.login(mail_info["username"], mail_info["password"])

        msg = MIMEText(mail_info["mail_text"], "plain", mail_info["mail_encoding"])
        msg["Subject"] = Header(mail_info["mail_subject"], mail_info["mail_encoding"])
        msg["from"] = mail_info["from"]
        msg["to"] = mail_info["to"]

        smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string())
        smtp.quit()


i = 0
while True:
    try:
        SendEamil()
    except:
        print("Error: 无法发送邮件")
    time.sleep(30)
    i += 1
    if i == 10:
        break
